'''
Created on Oct 19, 2010

@author: oabalbin
'''
import os
import RNAseq.sampleinfo.sampleDB as db
from collections import defaultdict, deque

def get_sample_seqfiles(dirpaths):
    
    mctp_namepattern='''s_%(lane)s_%(matepair)s_sequence.txt.gz'''
    pairs=['1','2']
    for sp in dirpaths:
        for fc, path in dirpaths[sp].iteritems():
            seq_files_list=deque()
            lane=fc.split('_')[1]
            #print sp, fc, path 
            for p in pairs:
                bothpairs=0
                vals= {'lane':lane,'matepair':p}
                seq_file = path+mctp_namepattern%vals
                if (os.path.isfile(seq_file)):
                    print 'File is present', seq_file
                    seq_files_list.append(seq_file)
                    bothpairs+=1
                else:
                    print 'File was not found', seq_file
            
            seq_files_list.append(bothpairs)
            yield seq_files_list


def get_sample_seq_fileslist(dirpaths):
    
    mctp_namepattern='''s_%(lane)s_%(matepair)s_sequence.txt.gz'''
    pairs=['1','2']
    seq_files_dict=defaultdict(deque)
    for sp in dirpaths:
        for fc, path in dirpaths[sp].iteritems():
            
            lane=fc.split('_')[1]
            #print sp, fc, path 
            for p in pairs:
                bothpairs=0
                vals= {'lane':lane,'matepair':p}
                seq_file = path+mctp_namepattern%vals
                if (os.path.isfile(seq_file)):
                    print 'File is present', seq_file
                    seq_files_dict[sp+'_'+fc].append(seq_file)
                    bothpairs+=1
                else:
                    print 'File was not found', seq_file
            
            seq_files_dict[sp+'_'+fc].append(bothpairs)
        
    return seq_files_dict


sample_info = open('/exds/users/oabalbin/projects/how_tos/example_samples_pools.txt')             
samples_file = '/exds/users/oabalbin/projects/alignment_test/pair_prostate_samples'
inputfile_name = '/exds/users/oabalbin/projects/sampleDB_database_10_16_10.csv'
outfile_name = '/exds/users/oabalbin/projects/alignment_test/sample_dirpaths.txt'

# custom because some Oncoseq samples are name inconsistent with sampleDB
samples_fc = db.get_flow_cell_dict(sample_info)
###
flowcell_dict=db.read_sampleDB_flowcell(inputfile_name)
dirpaths = db.get_flowcell_path_flowcell(flowcell_dict, samples_fc)

for i in range(len(dirpaths)):
    seqlist=get_sample_seqfiles(dirpaths)
    for i in seqlist:
        print i  
    